# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from urllib.error import URLError

import altair as alt
import pandas as pd
import openpyxl,os,json

import streamlit as st
from streamlit.hello.utils import show_code
from io import StringIO

def getSheets(target_excel_file_path):
    #获取表格sheet，此处不做数据处理
    if target_excel_file_path is not None:
        #新建字典用于存储对应sheet
        target_excel_file_sheets = {}
        #导入表格：表格内容较多时，该库导入速度较慢。待优化。
        target_excel_file = openpyxl.load_workbook(os.path.abspath(target_excel_file_path),read_only=False)
        #获取excel文件内所有表格名
        target_excel_file_sheet_names = target_excel_file.sheetnames
        for item in target_excel_file_sheet_names:
            #该字典键为sheet名，值为sheet具体内容
            target_excel_file_sheets.update({item: target_excel_file.get_sheet_by_name(item)})
        target_excel_file.close()
        return target_excel_file_sheets

def data_frame_demo():
    #@st.cache_data
    def get_UN_data(file_name):
        #AWS_BUCKET_URL = "https://streamlit-demo-data.s3-us-west-2.amazonaws.com"
        df = getSheets(file_name)
        
        return df

    try:
        uploaded_file = st.file_uploader("选择需求文件",type=['xlsx','xls'])
        uploaded_white_list = st.file_uploader("白名单文件",type=['xlsx','xls'])
        if uploaded_file is not None:
            # Can be used wherever a "file-like" object is accepted:
            print('地址：',str(uploaded_file),'endl')
            #dataframe = get_UN_data(uploaded_file)
            
            #st.write(dataframe)
    except URLError as e:
        st.error(
            """
            **This demo requires internet access.**
            Connection error: %s
        """
            % e.reason
        )
    def new_pares():
        pass


st.set_page_config(page_title="Excel上传", page_icon="📊")
st.markdown("# Excel上传")
st.sidebar.header("Excel上传")
st.write(
    """请在此处上传ARXML需求EXCEL文件"""
)
data_frame_demo()

